package com.hx.collection;

import java.util.*;

/**
 * @author jxlgcmh
 * @create 2019-05-07 16:49
 */
public class HashMapDemo {
    static final int MAXIMUM_CAPACITY = 1 << 30;


    static Map<String,String> map = new HashMap<>();

   static  {
        map.put("a","b");
    }




    public static void main(String[] args) {


       Thread thread1 = new Thread(() ->{
           if (map.containsKey("a")){
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               map.remove("a");
           };
       });
       thread1.start();


        Thread thread2 = new Thread(() ->{
            if (map.containsKey("a")){
                map.remove("a");
            };
        });
        thread2.start();


    }


    public void resize(){
        int h;
        int result = (h = "abc".hashCode()) ^ (h >>> 16);
        System.out.println(result);

        int h1 = "abc".hashCode();
        int h2 = h1 >>> 16;
        int result2 = h1 ^ h2;

        System.out.println(result2);

        int index1 = result2 & (16-1);
        System.out.println("初始化下标: "+index1);
        int index2 = result2 & (32-1);
        System.out.println("第一次扩容下标: "+index2);
        int index3 = result2 & (64-1);
        System.out.println("第二次扩容下标: "+index3);
    }

    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

    public static void method1() {
        //运行时会报错
//		Map map1=new Hashtable();
//		map1.put(null,null);
        Map map = new HashMap();
        map.put("h", 1235);
        map.put("a", 1245);
        map.put("b", 1735);
        //遍历key
        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        //遍历value
        Collection values = map.values();
        for (Object value : values) {
            System.out.println(value);
        }

        //同时遍历
        Set set = map.entrySet();
        Iterator iterator1 = set.iterator();
        while (iterator1.hasNext()) {
            Object next = iterator1.next();
            Map.Entry entry = (Map.Entry) next;
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        //
        System.out.println(map);
    }
}
